<%@page import="html.ATarget"%>
<%@page import="localization.LocalizationResource"%>
<%@page import="localization.Localization"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="strategies.Strategy"%>
<%@page import="strategies.StrategySequence"%>
<%@page import="strategies.WebContext"%>
<%@page import="templates.PresentationTemplate"%>
<%@page import="common.Initializer"%>
<%@page import="dao.Property"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.TreeMap"%>
<%@page import="java.util.Map"%>
<%@page import="dao.Entity"%>
<%@page import="database.DatabaseException"%>
<%@page import="presentation.PresentationElement"%>
<%@page import="formatters.ViewFormatter"%>
<%@page import="formatters.Formatter"%>
<%@page import="common.EntityPresentation"%>
<%@page import="dao.EntityDAO"%>
<%@page import="java.util.Iterator"%>
<%@page import="html.InputType"%>
<%@page import="templates.Template"%>
<%@page import="common.ParamStorage"%>
<%@page import="html.XHtmlFormatter"%>
<%@page import="common.Application"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%!    Application app = Application.getInstance();
    short currentTable = 0;
    XHtmlFormatter provider;
%>

<%
            ParamStorage storage = new ParamStorage(request);
            if (storage.hasParam("localization")) {
                Localization.bind(Short.parseShort(storage.getParam("localization")));
            }
            if (storage.hasParam("add") || storage.hasParam("edit")) {
                pageContext.getServletContext().getRequestDispatcher("/edit.jsp").forward(request, response);
            }


            /*InitialContext ic = new InitialContext();
            ic.lookup("java:comp/env/ejb/QueryInitializer");*/
            //ic.lookup("java:global/database-admin-pro/database-admin-ejb/QueryInitializerBean!beans.QueryInitializerBeanLocalHome");
            provider = new XHtmlFormatter(out);
            provider.startHtml();
            provider.startHead();
            provider.writeMeta("Content-Language", "en-US");
            provider.writeMeta("Content-Type", "text/xhtml+xml; charset=iso-8859-1");
            provider.writeLink("stylesheet", "text/css", "style.css");
            provider.writeLink("shortcut icon", "image/png", "database-icon.png");
            provider.writeScript("text/javascript", "lib.js");
            provider.writeTitle(Localization.getString(app.getTitle()));
            provider.endHead();
            provider.startBody();
            provider.startDiv("header");
            provider.startDiv();
            provider.writeH1(Localization.getString(app.getTitle()));
            provider.endDiv();
            provider.endDiv();
            provider.startForm("index.jsp", "post");
            provider.startDiv("control");
            if (storage.hasParam("table-selection")) {
                currentTable = Short.parseShort(storage.getParam("table-selection"));
            }
            boolean ignoreCustomStrategy = false;
            Iterable<Template> templateCollection = app.getTemplateCollection();
            for (Template t : templateCollection) {
                if (storage.hasParam("table-" + Short.toString(t.getUid()))) {
                    currentTable = t.getUid();
                    ignoreCustomStrategy = true;
                    break;
                }
            }
            provider.writeInput(InputType.hidden, "table-selection", "", "", Short.toString(currentTable));
            provider.startDiv("", "selectPanel");
            boolean first = true;
            Iterator<Template> it = templateCollection.iterator();
            while (it.hasNext()) {
                Template t = it.next();
                String styleClass = "tab";
                if (first) {
                    styleClass = "left-tab";
                    first = false;
                }
                if (!it.hasNext()) {
                    styleClass = "right-tab";
                }
                if (currentTable == t.getUid())
                    styleClass += "-active";
                provider.writeInput(InputType.submit, "table-" + Short.toString(t.getUid()), "", styleClass, Localization.getString(t.getName()));
            }
            provider.startA(Localization.getHelpFileName(), ATarget.Blank);
            provider.startDiv("help");
            provider.putImage("pdf.png", "large");
            provider.echo(Localization.getString("GetHelp"));
            provider.endDiv();
            provider.endA();
            provider.endDiv();
            provider.startDiv("", "optionsPanel");
            provider.writeInput(InputType.submit, "delete", "delete", "", Localization.getString("Delete"));
            provider.writeInput(InputType.submit, "add", "add", "", Localization.getString("Add"));
            provider.writeInput(InputType.submit, "edit", "edit", "", Localization.getString("Edit"));
            Iterable<LocalizationResource> resources = Localization.getResources();
            provider.startSelect("localization", "localization");
            for (LocalizationResource res: resources) {
                provider.putSelectOption(Short.toString(res.getUid()), res.getLanguage(), res.getUid() == Localization.getResourceUid());
            }
            provider.endSelect();
            provider.endDiv();
            provider.endDiv();
            provider.startDiv("content");

            WebContext ctx = new WebContext(provider, storage);
            ctx.setUid(currentTable);
            StrategySequence strategySequence = new StrategySequence(request, ignoreCustomStrategy);
            Iterable<Strategy> sequence = strategySequence.getSequence();
            for (Strategy s: sequence) {
                s.execute(ctx);
                if (ctx.isRedirect())
                    break;
            }

            provider.startScript("text/javascript");
            if (currentTable != 0)
                provider.echo("document.getElementById('add').style.display = 'inline';");
            else
                provider.echo("document.getElementById('add').style.display = 'none';");
            provider.endScript();

            if (ctx.isRedirect()) {
                String url = ctx.getRedirect();
                ctx.setRedirect("");
                pageContext.getServletContext().getRequestDispatcher(url).forward(request, response);
            }
            
            provider.endDiv();
            provider.endForm();
            provider.startDiv("footer");
            provider.writeP("center", Localization.getString("Copyright"));
            provider.endDiv();
            provider.endBody();
            provider.endHtml();
%>
